using System.Collections.Generic;
using System.Linq;
using System.Utility.System;
using System.Windows.Media;

namespace System.Utility.Helper
{
	public class DataVisualAnalysis
	{
		public DataVisualAnalysis()
		{
		}

		public static void ColorDistributeAnalysis<TItem>(List<TItem> items, Color start, Color end, string property, string attach, bool attachColor = true)
		where TItem : IDynamicEnable
		{
			if ((items == null ? false : items.Count > 0))
			{
				short[] num = new short[3];
				byte r = start.R;
				num[0] = TypeExtension.ToInt16(r.ToString());
				r = start.G;
				num[1] = TypeExtension.ToInt16(r.ToString());
				r = start.B;
				num[2] = TypeExtension.ToInt16(r.ToString());
				short[] numArray = num;
				num = new short[3];
				r = end.R;
				num[0] = TypeExtension.ToInt16(r.ToString());
				r = end.G;
				num[1] = TypeExtension.ToInt16(r.ToString());
				r = end.B;
				num[2] = TypeExtension.ToInt16(r.ToString());
				short[] numArray1 = num;
				int num1 = Math.Abs((int)(Enumerable.Max(numArray) - Enumerable.Min(numArray1))) / 2;
				List<double> nums = new List<double>();
				items.ForEach((TItem item) => {
					if (!((object)item is DynamicX))
					{
						double safeDouble = TypeExtension.ToSafeDouble(TypeExtension.ToSafeString(Reflection.GetObjectValue<TItem>(item, property)));
						nums.Add(safeDouble);
					}
					else
					{
						nums.Add(TypeExtension.ToSafeDouble(TypeExtension.ToSafeString(((object)item as DynamicX).Get(property))));
					}
				});
				double num2 = Enumerable.Max(nums);
				double num3 = Enumerable.Min(nums);
				double num4 = num2 - num3;
				SetStepColor<TItem>(items, ref start, attach, attachColor, numArray, numArray1, num1, nums, num3, num4);
			}
		}

		public static void RangeAnalysis(IEnumerable<object> oitems, double scale, string property, string attach)
		{
			if ((oitems == null ? false : Enumerable.Any(oitems)))
			{
				List<object> list = Enumerable.ToList(oitems);
				if (TypeExtension.IsInstanceOfT<IDynamicEnable>(Enumerable.First(list)))
				{
					int num3 = Enumerable.Count(list);
					double[] numArray1 = new double[num3];
					int num4 = 0;
					list.ForEach((object item) => {
						double num = 0;
						num = (!(item is DynamicX) ? TypeExtension.ToSafeDouble(TypeExtension.ToSafeString(Reflection.GetObjectValue<object>(item, property))) : TypeExtension.ToSafeDouble(TypeExtension.ToSafeString((item as DynamicX).Get(property))));
						double[] numArray = numArray1;
						int num1 = num4;
						int num2 = num1;
						num4 = num1 + 1;
						numArray[num2] = num;
					});
					double num5 = Enumerable.Max(numArray1);
					num4 = 0;
					while (num4 < num3)
					{
						IDynamicEnable dynamicX = list[num4] as IDynamicEnable;
						if (dynamicX != null)
						{
							if (dynamicX.Dynamic == null)
							{
								dynamicX.Dynamic = new DynamicX();
							}
							double num6 = numArray1[num4] / num5 * scale;
							dynamicX.Dynamic.Set(attach, num6 + 0.5);
							num4++;
						}
						else
						{
							return;
						}
					}
				}
			}
		}

		private static void SetStepColor<TItem>(List<TItem> items, ref Color start, string attach, bool attachColor, short[] s, short[] e, int maxStep, List<double> values, double min, double sub)
		where TItem : IDynamicEnable
		{
			TItem item;
			Color color = start;
			if (sub != 0)
			{
				int num = (int)Math.Floor((sub > (double)maxStep ? (double)maxStep : sub));
				double num1 = sub / (double)num;
				int count = items.Count;
				for (int i = 0; i < count; i++)
				{
					int num2 = (int)Math.Floor((values[i] - min) / num1);
					int[] numArray = new int[3];
					for (int j = 0; j < 3; j++)
					{
						numArray[j] = e[j] + (s[j] - e[j]) / num * num2;
					}
					if (items[i].Dynamic == null)
					{
						items[i].Dynamic = new DynamicX();
					}
					if (!attachColor)
					{
						item = items[i];
						item.Dynamic.Set(attach, new SolidColorBrush(Color.FromRgb(BitConverter.GetBytes(numArray[0])[0], BitConverter.GetBytes(numArray[1])[0], BitConverter.GetBytes(numArray[2])[0])));
					}
					else
					{
						item = items[i];
						item.Dynamic.Set(attach, Color.FromRgb(BitConverter.GetBytes(numArray[0])[0], BitConverter.GetBytes(numArray[1])[0], BitConverter.GetBytes(numArray[2])[0]));
					}
				}
			}
			else
			{
				items.ForEach((TItem m) => {
					if (m.Dynamic == null)
					{
						m.Dynamic = new DynamicX();
					}
					if (!attachColor)
					{
						m.Dynamic.Set(attach, new SolidColorBrush(color));
					}
					else
					{
						m.Dynamic.Set(attach, color);
					}
				});
			}
		}
	}
}